package com.foreknow.elm.controller;

import com.foreknow.elm.dao.DeliveryaddressDao;
import com.foreknow.elm.dao.impl.DeliveryaddressDaoImpl;
import com.foreknow.elm.po.Deliveryaddress;
import com.foreknow.elm.service.BusinessService;
import com.foreknow.elm.service.DeliveryaddressService;
import com.foreknow.elm.service.impl.BusinessServiceImpl;
import com.foreknow.elm.service.impl.DeliveryaddressServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.List;

public class DeliveryaddressController {
    //  DeliveryaddressController/listDeliveryAddressByUserId?userId=12345672222
    public Object listDeliveryAddressByUserId(HttpServletRequest request){
        String userId = request.getParameter("userId");
        DeliveryaddressService deliveryaddressService = new DeliveryaddressServiceImpl();
        List<Deliveryaddress> list = deliveryaddressService.listDeliveryAddressByUserId(userId);
        return list;
    }

    //  DeliveryaddressController/getDeliveryAddressById?daId=2
    public Deliveryaddress getDeliveryAddressById(HttpServletRequest request){
        Integer daId = Integer.parseInt(request.getParameter("daId"));
        DeliveryaddressService deliveryaddressService = new DeliveryaddressServiceImpl();
        Deliveryaddress deliveryaddress = null;

        deliveryaddress = deliveryaddressService.getDeliveryAddressById(daId);

        return deliveryaddress;
    }
    //  DeliveryaddressController/saveDeliveryAddress?contactName="习大大"&contactSex=1&contactTel=646513548&address="中南海"&userId=146813156
    public int saveDeliveryAddress(HttpServletRequest request){
        String contactName = request.getParameter("contactName");
        Integer contactSex = Integer.parseInt(request.getParameter("contactSex"));
        String contactTel = request.getParameter("contactTel");
        String address = request.getParameter("address");
        String userId = request.getParameter("userId");
        DeliveryaddressService deliveryaddressService = new DeliveryaddressServiceImpl();
        int i = deliveryaddressService.saveDeliveryAddress(contactName,contactSex,contactTel,address,userId);
        return i;
    }

    //  DeliveryaddressController/updateDeliveryAddress?contactName="习大"&contactSex=1&contactTel=646513548&address="中地中南海"&userId=146813156&daId=18
    public int updateDeliveryAddress(HttpServletRequest request){
        Integer daId = Integer.parseInt(request.getParameter("daId"));
        String contactName = request.getParameter("contactName");
        Integer contactSex = Integer.parseInt(request.getParameter("contactSex"));
        String contactTel = request.getParameter("contactTel");
        String address = request.getParameter("address");
        String userId = request.getParameter("userId");
        DeliveryaddressService deliveryaddressService = new DeliveryaddressServiceImpl();
        int i = deliveryaddressService.updateDeliveryAddress(daId,contactName,contactSex,contactTel,address,userId);
        return i;
    }
    //  DeliveryaddressController/removeDeliveryAddress?daId=2
    public int removeDeliveryAddress(HttpServletRequest request) {
        Integer daId = Integer.parseInt(request.getParameter("daId"));
        DeliveryaddressService deliveryaddressService = new DeliveryaddressServiceImpl();
        int i = deliveryaddressService.removeDeliveryAddress(daId);
        return i;
    }
}
